package com.qf.controller;



import com.qf.entity.Item;
import com.qf.exception.BizException;
import com.qf.exception.ControllerException;
import com.qf.service.ItemService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/item")
public class ItemController {
    private ItemService itemService;

    public ItemController(ItemService itemService) {
        this.itemService = itemService;
    }

    @GetMapping
    public Object getAll(){

        List<Item> items=null;
        try {
            items= itemService.queryAll();
        }catch (BizException e){
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw  new ControllerException("控制层商品种类查询异常："+e.getMessage());
        }

        return items;
    }



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Item selectOne(Integer id) {
        return this.itemService.queryById(id);
    }


    @GetMapping("getAllItem")
    public List<Item> getAllItem() {
        List<Item> all = itemService.getAll();
        return all;

    }

    @RequestMapping("delItem")
    public int delItem(int id) {
        int i = itemService.chackGoods(id);
        if (i == 0) {
            itemService.deleteById(id);
            return 0;
        } else {
            return 1;
        }
    }

    @RequestMapping("updItem")
    public int updItem(Item item) {
        int i = itemService.chackGoods(item.getId());
        if (i == 0) {
            itemService.update(item);
            return 0;
        }else {
            return 1;
        }

    }

    @RequestMapping("addItem")
    public Item addItem(Item item) {
        return itemService.insert(item);
    }


}

